<?php 
namespace FrontEnd\Model;

 use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Predicate\In;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Predicate\Like;

use Zend\Cache\Storage\StorageInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
		
 class ProductTable extends AbstractTableGateway
 {
     protected $tableGateway;
     protected $cache;
     protected $table = 'product';
     //protected $resultSetPrototype;
     
     public function __construct(Adapter $adapter,TableGateway $tableGateway)
     {
         $this->tableGateway = $tableGateway;
         
         $this->adapter = $adapter;
         $this->resultSetPrototype = new HydratingResultSet();
        $this->resultSetPrototype->setObjectPrototype(new FrontEnd());
         $this->initialize();
     }

     public function setCache(StorageInterface $cache)
     {
     	$this->cache = $cache;
     }
     
     public function fetchAll($url)
     {
     	if( ($arr = $this->cache->getItem($url)) == null) {
		         $select = $this->tableGateway->getSql()->select()->order ( array (
						'product.ID_PR DESC' 
				) );
		         $select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
		         $select->group('product.ID_PR');
		     	$resultSet = $this->tableGateway->selectWith ( $select );
		     	$arr = $resultSet->buffer ()->toArray();
		     	$this->cache->setItem($url,  $arr );
     	}
     	return $arr;
         
     }

     
     public function countAll() {
     	$select = $this->tableGateway->getSql()->select();
         $select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
         $select->group('product.ID_PR');
     	$resultSet = $this->tableGateway->selectWith ( $select )->count();
     	return $resultSet;
     }
     
     public function fetchSearchAll($offset,$limit,$search,$url)
     {
     	if( ($arr = $this->cache->getItem($url)) == null) {
		     	$where = new \Zend\Db\Sql\Where();
		     	$like_name = $where->like('NAME_PR','%'.$search.'%');
		     	$where->OR;
		     	$like_content = $where->like('CONTENT_PR','%'.$search.'%');
		     	$where->OR;
		     	$like_highlight = $where->like('HIGHLIGHT_PR','%'.$search.'%');
		     	$where->OR;
		     	$like_id = $where->like('product.ID_PR','%'.$search.'%');
		     	$select = $this->tableGateway->getSql()->select()->order ( array (
		     			'product.ID_PR DESC'
		     	) );
		     	$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
		     	$select->limit($limit)->offset($offset);
		     	$select->group('product.ID_PR');
		     	$select->where($where);
		     	$resultSet = $this->tableGateway->selectWith ( $select );
		     	$arr = $resultSet->buffer ()->toArray();
				$this->cache->setItem($url,  $arr );
     	}
     	return $arr;
     }
     
     public function countSearchAll($search) {
     	$where = new \Zend\Db\Sql\Where();
     	$like_name = $where->like('NAME_PR','%'.$search.'%');
     	$where->OR;
     	$like_content = $where->like('CONTENT_PR','%'.$search.'%');
     	$where->OR;
     	$like_highlight = $where->like('HIGHLIGHT_PR','%'.$search.'%');
     	$where->OR;
     	$like_id = $where->like('product.ID_PR','%'.$search.'%');
     	$select = $this->tableGateway->getSql()->select();
     	$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     	$select->group('product.ID_PR');
     	$select->where($where);
     	$resultSet = $this->tableGateway->selectWith ( $select )->count();
     	return $resultSet;
     }
     
     public function getProduct($id)
     {
         $id  = (int) $id;
         $rowset = $this->tableGateway->select(array('ID_PR' => $id));
         $row = $rowset->current();
        
         if ($row) {
             $row->namepro = $row->NAME_PR;
	         $row->contentpro = $row->CONTENT_PR;
	         $row->activepro = $row->ACTIVE_PR;
	         $row->producttypes = $row->ID_PRT;
	         $row->idproduct = $row->ID_PR;
         }        
         return $row;     	
     }

     public function getProductInType($idtype)
     {
     	$idtype  = (int) $idtype;
     	$resultSet = $this->tableGateway->select(array('ID_PRT' => $idtype));
		$resultSet = $resultSet->toArray();
		$arr = array();
		foreach ($resultSet as $val)
			$arr[] = $val['ID_PR'];
		return $arr;
     }
     
     public function saveProduct(FrontEnd $product,$id)
     {
     	if($product->activepro == null)
     		$product->activepro = 0;
         $data = array(
         	 'ID_PRT' => $product->producttypes,
			 'NAME_PR' => $product->namepro ,
			 'CONTENT_PR' => $product->contentpro ,
			 'ACTIVE_PR' => $product->activepro ,
			 'DATE_PR' => date("d-m-Y") ,
         );

         if ($id == 0) {
             $this->tableGateway->insert($data);
             $select = $this->tableGateway->getSql()->select();
             $select->columns(array(
                   'ID_PR' => new Expression('MAX(ID_PR)')
	          ));
	         $resultSet = $this->tableGateway->selectWith ( $select );
	         $row = $resultSet->current()->ID_PR;
	         return  $row;
         }else {
             if ($this->getProduct($id)) {
                 $this->tableGateway->update($data, array('ID_PR' => $id));
             } else {
                 throw new \Exception('FrontEnd id does not exist');
             }
         }
     }

     public function updatehighlights($id,$idquery){
     	if($idquery == 'no')
     		$idquery='yes';
     	else 
     		$idquery='no';
     	$data = array(
     			'HIGHLIGHT_PR' => $idquery,
     	);
     	$this->tableGateway->update($data, array('ID_PR' => $id));
     }
     
     public function deleteProduct($id)
     {
         $this->tableGateway->delete(array('ID_PR' => (int) $id));
     }
     
     public function deleteAllProduct($arrid)
     {
     	$where = new In('ID_PR',$arrid);
     	$this->tableGateway->delete($where);
     }
     
     public function deleteProductInProductType($id)
     {
     	$this->tableGateway->delete(array('ID_PRT' => (int) $id));
     }
 }